package cn.iocoder.an.module.mold.service.recheckcat2item;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.module.mold.controller.admin.recheckcat2item.vo.RecheckCat2itemSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.an.module.mold.controller.admin.recheckcat2item.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.recheckcat2item.RecheckCat2itemDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.mold.dal.mysql.recheckcat2item.RecheckCat2itemMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具复检分类项目对照 Service 实现类
 *
 * @author ykk
 */
@Service
@Validated
public class RecheckCat2itemServiceImpl implements RecheckCat2itemService {

    @Resource
    private RecheckCat2itemMapper recheckCat2itemMapper;

    @Override
    public Long createRecheckCat2item(RecheckCat2itemSaveReqVO createReqVO) {
        // 插入RecheckCat2itemDO
        RecheckCat2itemDO recheckCat2item = BeanUtils.toBean(createReqVO, RecheckCat2itemDO.class);
        recheckCat2itemMapper.insert(recheckCat2item);

        // 返回
        return recheckCat2item.getId();
    }

    @Override
    public void updateRecheckCat2item(RecheckCat2itemSaveReqVO updateReqVO) {
        // 校验存在
        validateRecheckCat2itemExists(updateReqVO.getId());
        // 更新
        RecheckCat2itemDO updateObj = BeanUtils.toBean(updateReqVO, RecheckCat2itemDO.class);
        recheckCat2itemMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecheckCat2item(Long id) {
        // 校验存在
        validateRecheckCat2itemExists(id);
        // 删除
        recheckCat2itemMapper.deleteById(id);
    }

    @Override
        public void deleteRecheckCat2itemListByIds(List<Long> ids) {
        // 删除
        recheckCat2itemMapper.deleteByIds(ids);
        }


    private void validateRecheckCat2itemExists(Long id) {
        if (recheckCat2itemMapper.selectById(id) == null) {
            throw exception(RECHECK_CAT2ITEM_NOT_EXISTS);
        }
    }

    @Override
    public RecheckCat2itemDO getRecheckCat2item(Long id) {
        return recheckCat2itemMapper.selectById(id);
    }

    @Override
    public PageResult<RecheckCat2itemDO> getRecheckCat2itemPage(RecheckCat2itemPageReqVO pageReqVO) {
        return recheckCat2itemMapper.selectPage(pageReqVO);
    }



    @Override
    public List<RecheckCat2itemDO> getRecheckCat2itemByCatId(Long catId) {
        return recheckCat2itemMapper.selectList(
                new LambdaQueryWrapper<RecheckCat2itemDO>().eq(RecheckCat2itemDO::getCategoryId, catId)
        );
    }


    @Override
    public boolean deleteRecheckCat2itemByCatId(Long catId) {
        // 使用原生SQL删除category_id为catId的所有记录
        return recheckCat2itemMapper.deleteByCategoryId(catId) > 0;
    }

    public void updateRecheckCat2itemByCatId(List<RecheckCat2itemSaveReqVO> updateReqVO) {
        if (CollUtil.isEmpty(updateReqVO)) {
            return;
        }

        // 获取categoryId（假设所有记录的categoryId相同）
        Long categoryId = updateReqVO.get(0).getCategoryId();

        // 先删除该categoryId下的所有记录
        recheckCat2itemMapper.delete(
                new LambdaQueryWrapper<RecheckCat2itemDO>()
                        .eq(RecheckCat2itemDO::getCategoryId, categoryId)
        );

        // 批量插入新记录
        List<RecheckCat2itemDO> toInsertList = convertList(updateReqVO,
                vo -> BeanUtils.toBean(vo, RecheckCat2itemDO.class));

        toInsertList.forEach(recheckCat2itemMapper::insert);
    }

}